const router = require('koa-router')()

const { loginRedirect, logincheck } = require('../middleware/loginChecks')

router.get('/', loginRedirect, async (ctx, next) => {
  // 这里的渲染是异步的，必须要等模板加载完毕之后，才能进行变量的赋值
  await ctx.render('index', {
    title: 'Hello Koa 2!',
    msg: '你好！',
    userName: 'EastM',
    isMe: true,
    blogList: [
      {
        id:'1',
        title: 'aaa'
      },
      {
        id: '2',
        title: 'bbb'
      },
      {
        id: '3',
        title: 'ccc'
      }
    ]
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  // console.log(ctx)
  // const session = ctx.session
  // if(session.viewName == null) {
  //   session.viewName = 0
  // }

  // session.viewName++

  ctx.body = {
    title: 'koa2 json',
    // viewName: session.vieName
  }
})

router.get('/profile/:userName', async (ctx ,next) => {
  const { userName } = ctx.params
  ctx.body = {
    userName
  }
})

router.get('/profile/:userName/:pageIndex', async (ctx, next) => {
  const { userName, pageIndex } = ctx.params
  ctx.body = {
    userName,
    pageIndex
  }
})

module.exports = router
